import { Component, OnInit, Inject } from '@angular/core'
import { HttpClient } from '@angular/common/http'
import { MAT_DIALOG_DATA } from '@angular/material'
import { Observable } from 'rxjs'

@Component({
  selector: 'MessageDialog',
  templateUrl: './message-dialog.component.html',
  styleUrls: ['./message-dialog.component.less']
})
export class MessageDialogComponent implements OnInit {

  messageData: MyMessageData
  constructor(@Inject(MAT_DIALOG_DATA) public data: any,
    private httpClient: HttpClient) {
    this.messageData = data
  }

  submit(): Observable<any> {
    return this.httpClient.post(
      this.data.postData.url,
      this.data.postData.body
    )
  }

  makeBody(i: number) {
    const fields = this.data.inputFields as Array<any>
    this.messageData.inputFields[i].value =
      (<HTMLInputElement>document.getElementsByName(fields[i].name)[0]).value
  }

  submitInputForm() {
    return this.httpClient.post(
      this.data.postData.url,
      {
        data: this.messageData.postData.body,
        inputFields: this.messageData.inputFields
      }
    )
  }

  ngOnInit() {
  }

}
export class MyMessageDetail {
  text: string
  confirmText: string
}
export class MyPostData {
  url: string
  header: string[] | null
  body: any
}
export class MyInputField {
  name: string
  placeHolder: string
  type: string
  value: any
}
export class MyMessageData {
  /**
   * @param type  'messageOnly' | 'confirmForm' | 'inputForm'
   */
  constructor(type: string) {
    this.messageOnly = false
    this.isConfirmForm = false
    this.isInputForm = false
    switch (type) {
      case 'messageOnly':
        this.messageOnly = true
        break
      case 'confrimForm':
        this.isConfirmForm = true
        break
      case 'inputForm':
        this.isInputForm = true
        break
      default:
        break
    }
    this.messageDetail = new MyMessageDetail()
    this.postData = new MyPostData()
    this.inputFields = new Array<MyInputField>()
  }
  isConfirmForm: boolean | false
  messageOnly: boolean | true
  isInputForm: boolean | false
  messageDetail: MyMessageDetail
  /**
   * 请求服务器的请求信息
   */
  postData: MyPostData
  inputFields: Array<MyInputField>
}
